package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.ShoppingCart;
import com.itheima.reggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @Author Pan Rundong
 * @Date 2023-01-31 17:59
 * @Desc 购物车控制器
 */
@Slf4j
@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     * 添加购物车
     * @Author: Pan Rundong
     * @Date: 2023-02-01 21:35
     * @Greet2u: (O ` 3 ′ O)~~♡   ( °ー°〃)
     * @param shoppingCart
     * @return com.itheima.reggie.common.R<com.itheima.reggie.entity.ShoppingCart>
     */
    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart) {
        log.info(shoppingCart.toString());

        //设置用户id，指定其为哪个用户的购物车
        Long userId = BaseContext.getCurrentId();
        shoppingCart.setUserId(userId);

        //查询当前菜品或套餐是否在购物车中
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(shoppingCart.getDishId() != null,
                ShoppingCart::getDishId, shoppingCart.getDishId());
        queryWrapper.eq(shoppingCart.getSetmealId() != null,
                ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        queryWrapper.eq(ShoppingCart::getUserId, userId);

        ShoppingCart one = shoppingCartService.getOne(queryWrapper);

        //判断
        if(one != null){
            //若存在，则数量+1
            one.setNumber(one.getNumber()+1);
            shoppingCartService.updateById(one);
        }else {
            //若不存在，添加入购物车
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartService.save(shoppingCart);
            one = shoppingCart;
        }
        return R.success(one);
    }

    /**
     * 查看购物车
     * @Author: Pan Rundong
     * @Date: 2023-02-02 8:51
     * @Greet2u: (O ` 3 ′ O)~~♡   ( °ー°〃)
     * @return com.itheima.reggie.common.R<java.util.List < com.itheima.reggie.entity.ShoppingCart>>
     */
    @GetMapping("/list")
    public R<List<ShoppingCart>> list() {
        log.info("查看购物车");
        //获得用户id
        Long userId = BaseContext.getCurrentId();
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId, userId);
        queryWrapper.orderByDesc(ShoppingCart::getCreateTime);

        List<ShoppingCart> list = shoppingCartService.list(queryWrapper);
        return R.success(list);
    }

    /**
     * 清空购物车
     * @Author: Pan Rundong
     * @Date: 2023-02-02 9:15
     * @Greet2u: (O ` 3 ′ O)~~♡   ( °ー°〃)
     * @return com.itheima.reggie.common.R<java.lang.String>
     */
    @DeleteMapping("/clean")
    public R<String> deleteList() {
        //设置条件，以用户名id为条件
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());

        //清空
        shoppingCartService.remove(queryWrapper);
        return R.success("清空购物车成功");
    }

    /**
     * 减少商品
     * @Author: Pan Rundong
     * @Date: 2023-02-02 9:33
     * @Greet2u: (O ` 3 ′ O)~~♡   ( °ー°〃)
     * @param shoppingCart
     * @return com.itheima.reggie.common.R<com.itheima.reggie.entity.ShoppingCart>
     */
    @PostMapping("/sub")
    public R<ShoppingCart> sub(@RequestBody ShoppingCart shoppingCart) {
        //获得用户id以确认是谁的购物车
        Long userId = BaseContext.getCurrentId();
        //设置条件，userId + dishId/setmealId
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(shoppingCart.getDishId() != null,
                ShoppingCart::getDishId, shoppingCart.getDishId());
        queryWrapper.eq(shoppingCart.getSetmealId() != null,
                ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        queryWrapper.eq(ShoppingCart::getUserId, userId);

        ShoppingCart one = shoppingCartService.getOne(queryWrapper);

        //判断数量
        one.setNumber(one.getNumber()-1);
        if (one.getNumber() > 0) {
            //大于1，数量减1
            shoppingCartService.updateById(one);
        } else if (one.getNumber() == 0) {
            //等于1，删除
            shoppingCartService.removeById(one);
        } else {
            //其他，报异常
            throw new CustomException("商品不存在");
        }
        return R.success(one);
    }
}
